package org.batatasframework.support.database.parameters;

import java.sql.Date;
import java.sql.PreparedStatement;

public class DatetimeParamenter extends AbstractQueryParameter<Date>{
	
	//private final Date value;
	
	public DatetimeParamenter(int index, Date value){
		super(index, value);
	}

	public DatetimeParamenter(int index, java.util.Date value){
		super(index, new Date(value.getTime()));
	}
	
	private static final Date valueOf(Object value){
		if(value instanceof String){
			return Date.valueOf(value.toString());
		}else if(value instanceof Date){
			return (Date)value;
		}else if(value instanceof java.util.Date){
			return new Date(((java.util.Date)value).getTime());
		}else if(value instanceof Number){
			return new Date(((Number) value).longValue());
		}
		return Date.valueOf(value.toString());
	}
	
	public DatetimeParamenter(int index, Object value){
		super(index, valueOf(value));
	}

	public DatetimeParamenter(int index, String value){
		super(index, Date.valueOf(value));
	}

	
	@Override
	public void set(PreparedStatement statement) throws Exception {
		statement.setDate(index, value);
	}

}
